package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.CaiDetail;
import com.jemmyee.pizza.entity.CaiType;
import com.jemmyee.pizza.entity.User;
import com.jemmyee.pizza.service.ICaiDetailService;
import com.jemmyee.pizza.service.ICaiTypeService;
import com.jemmyee.pizza.service.IUserService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.CaiBean;


/**
 * @Description:部门管理
 * @author:jemmyee@gmail.com
 * @date:2010-10-28
 * @version:v1.0
 */
@Controller
@RequestMapping("/admin/caidetail")
public class CaiDetailController {
	
	private static Log log= LogFactory.getLog(CaiDetailController.class);
	
	@Autowired
	private ICaiDetailService<CaiDetail> caiDetailService;
	@Autowired
	private ICaiTypeService<CaiType> caiTypeService;
	@Autowired
	private IUserService<User> userService;
	

	@RequestMapping("/to_add")
	public ModelAndView toAdd(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/admin/caidetail/add");
		mv.addObject("types",caiTypeService.findByProperty("fatherId",0));
		return mv;
	}
	
	@RequestMapping("/cascade/{id}/{type}")
	public ModelAndView cascade(@PathVariable int id,@PathVariable int type){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/admin/caidetail/cascade"+type);
	    List<CaiType> types=caiTypeService.findByProperty("fatherId",id);
		mv.addObject("types",types);
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id,HttpServletRequest request){
		   ModelAndView mv=new ModelAndView();
		   mv.setViewName("/admin/caidetail/edit");
		   mv.addObject("types",caiTypeService.findByProperty("fatherId",0));
		   mv.addObject("caidetail",caiDetailService.findById(id));
		   return mv;
	}
	
	@RequestMapping(value="/detail/{id}")
	public ModelAndView detail(@PathVariable Integer id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/admin/caidetail/detail");
		mv.addObject("item",caiDetailService.findById(id));
		return mv;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(@RequestBody CaiDetail caiDetail,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		if(false)
		{
			model.put("msg","error");
		}else
		{
			caiDetail.setStatus(Short.valueOf("0"));
			caiDetail.setCreateTime(new Date());
			String uname=request.getSession().getAttribute(Constants.ONLINE_ADMIN).toString();
			caiDetail.setUserId(userService.findByProperty("name", uname).get(0).getId());
			if(caiDetail.getType4()!=null&&caiDetail.getType4()!=0){
				caiDetail.setTypeId(caiDetail.getType4());
			}
			else if(caiDetail.getType3()!=null&&caiDetail.getType3()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType3());
			}
			else if(caiDetail.getType2()!=null&&caiDetail.getType2()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType2());
			}
			else if(caiDetail.getType1()!=null&&caiDetail.getType1()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType1());
			}
			caiDetailService.save(caiDetail);
			model.put("msg","succ");
		}
		return model;
	}


	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody CaiDetail caiDetail,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		if(false)
		{
			model.put("msg","error");
		}else
		{
			CaiDetail temp=caiDetailService.findById(caiDetail.getId());
            temp.setCaiType(caiDetail.getCaiType());
            if(caiDetail.getLeixing()==1)
            {
                temp.setJieCount(caiDetail.getJieCount());
                temp.setJieMoney(caiDetail.getJieMoney());
            }
            else
            {
                temp.setDaiCount(caiDetail.getDaiCount());
                temp.setDaiMoney(caiDetail.getDaiMoney());
                temp.setDaiMan(caiDetail.getDaiMan());
                temp.setDaiDept(caiDetail.getDaiDept());
            }

            //temp.setHuansuan(caiDetail.getHuansuan());

            temp.setJiPingzheng(caiDetail.getJiPingzheng());
            temp.setYuanjinjia(caiDetail.getYuanjinjia());
            temp.setYuCount(temp.getYuCount());
            temp.setYuMoney(caiDetail.getYuMoney());
            temp.setRemark(caiDetail.getRemark());
            temp.setLeixing(caiDetail.getLeixing());
            if(caiDetail.getType4()!=null&&caiDetail.getType4()!=0){
				caiDetail.setTypeId(caiDetail.getType4());
			}
			else if(caiDetail.getType3()!=null&&caiDetail.getType3()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType3());
			}
			else if(caiDetail.getType2()!=null&&caiDetail.getType2()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType2());
			}
			else if(caiDetail.getType1()!=null&&caiDetail.getType1()!=0)
			{
				caiDetail.setTypeId(caiDetail.getType1());
			}
//			real.setRemark(role.getRemark());
			caiDetailService.update(temp);
			model.put("msg","succ");
		}
		return model;
	}

	
	@RequestMapping(value="/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute CaiBean caibean,HttpServletRequest request){
		  DetachedCriteria dc= DetachedCriteria.forClass(CaiDetail.class);
		  CaiBean bean=new CaiBean();
		  if(from.equals("form"))
		   {
			   bean=caibean;
			   request.getSession().setAttribute("cai_bean", bean);
		   }
		   if(from.equals("page"))
		   {
			   if(request.getSession().getAttribute("cai_bean")!=null)
			   {
				   bean=(CaiBean)request.getSession().getAttribute("cai_bean");
			   }
		   }
		   if(from.equals("menu"))
		   {
			   
		   }
		   //得到type值
		   if(caibean.getType4()!=null&&caibean.getType4()!=0){
				bean.setType(caibean.getType4());
			}
			else if(caibean.getType3()!=null&&caibean.getType3()!=0)
			{
				bean.setType(caibean.getType3());
			}
			else if(caibean.getType2()!=null&&caibean.getType2()!=0)
			{
				bean.setType(caibean.getType2());
			}
			else if(caibean.getType1()!=null&&caibean.getType1()!=0)
			{
				bean.setType(caibean.getType1());
			}
		   
		   if(bean.getType()!=null&&bean.getType()!=0){
			  dc.add(Restrictions.eq("typeId",bean.getType()));
			}
		   //发布时间
			if (bean.getStartTime()!=null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.ge("createTime",bean.getStartTime()));
				dc.add(Restrictions.le("createTime",bean.getEndTime()));
			}
			if (bean.getStartTime()!=null&&bean.getEndTime()== null) {
				dc.add(Restrictions.ge("createTime",bean.getStartTime()));
			}
			if (bean.getStartTime()==null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.le("createTime",bean.getEndTime()));
			}   
		
//		   dc.addOrder(Order.desc("createTime"));
		   ModelAndView mv=new ModelAndView();
		   Page page=caiDetailService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"createTime");
		   mv.addObject("page",page);
		   mv.addObject("bean",bean);
		   mv.addObject("types",caiTypeService.findByProperty("fatherId",0));
		   mv.setViewName("/admin/caidetail/list");
		   return mv;
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Integer id,HttpServletRequest request){
		caiDetailService.delete(caiDetailService.findById(id));
	    return "redirect:/admin/caidetail/list/menu/0";
	}

}
